from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from app.core.logger import logger, TraceID
import asyncio
from app.core.logger import init_logger, attach_sql_logger
from tasks.jobs import (
    heavy_task, 
    monitor_archives, 
    sync_wp_products, 
    sync_trading_pair
)
from app.core.session import async_engine

async def main():
    """
    调度任务
    """
    # 初始化日志记录器(任务模式)
    init_logger("task")
    attach_sql_logger(async_engine.sync_engine, "task")
    TraceID.set() # 初始化TraceID

    scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")

    # 每10秒执行一次
    scheduler.add_job(monitor_archives.monitor_archives, CronTrigger(second="*/10"))
    # 每10秒执行一次
    # scheduler.add_job(sync_wp_products.sync_wp_products, CronTrigger(second="*/10"))
    # 每10秒执行一次
    # scheduler.add_job(sync_trading_pair.sync_trading_pair, CronTrigger(second="*/10"))

    scheduler.start()
    logger.info("Scheduler started")

    # 保持运行
    while True:
        logger.info("Scheduler running")
        await asyncio.sleep(60)

if __name__ == "__main__":
    asyncio.run(main())
